package Servlet;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.websocket.Session;

import DAO.DBCon;
import DAO.Se_Out;

@WebServlet("/EBIChange")
public class EBIChange extends HttpServlet{
	private static final long serialVersionUID = 1L;
	
	String sql1,sql2;
	
	public EBIChange(){
		super();
	}
	protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		
		//用于在报名页面中显示已存在于数据库的信息
		HttpSession se = request.getSession();
		String workID = (String) se.getAttribute("workID");
		//开始向数据库更新完善的信息，添加判断语句确保保留不更改的数据
		String name = new String(request.getParameter("name").getBytes("iso-8859-1"), "utf-8");
		if(name.equals("")||name.length()==0)
		{
			name = (String) se.getAttribute("name");
		}
		String Dname = new String(request.getParameter("Dname").getBytes("iso-8859-1"), "utf-8");
		if(Dname.equals("")||Dname.length()==0)
		{
			Dname = (String) se.getAttribute("Dname");
		}
		String Pname = new String(request.getParameter("Pname").getBytes("iso-8859-1"), "utf-8");
		if(Pname.equals("")||Pname.length()==0)
		{
			Pname = (String) se.getAttribute("Pname");
		}
		String Pstatus = new String(request.getParameter("Pstatus").getBytes("iso-8859-1"), "utf-8");
		if(Pstatus.equals("")||Pstatus.length()==0)
		{
			Pstatus = (String) se.getAttribute("Pstatus");
		}
		String sex = new String(request.getParameter("sex").getBytes("iso-8859-1"), "utf-8");
		if(sex.equals("")||sex.length()==0)
		{
			sex = (String) se.getAttribute("sex");
		}
		String nation = new String(request.getParameter("nation").getBytes("iso-8859-1"), "utf-8");
		if(nation.equals("")||nation.length()==0)
		{
			nation = (String) se.getAttribute("nation");
		}
		String education = new String(request.getParameter("education").getBytes("iso-8859-1"), "utf-8");
		if(education.equals("")||education.length()==0)
		{
			education = (String) se.getAttribute("education");
		}
		String Mstatus = new String(request.getParameter("Mstatus").getBytes("iso-8859-1"), "utf-8");
		if(Mstatus.equals("")||Mstatus.length()==0)
		{
			Mstatus = (String) se.getAttribute("Mstatus");
		}
		String address = new String(request.getParameter("address").getBytes("iso-8859-1"), "utf-8");
		if(address.equals("")||address.length()==0)
		{
			address = (String) se.getAttribute("address");
		}
		String phone = new String(request.getParameter("phone").getBytes("iso-8859-1"), "utf-8");
		if(phone.equals("")||phone.length()==0)
		{
			phone = (String) se.getAttribute("phone");
		}
		String identify = request.getParameter("identify");
		if(identify.equals("")||identify.length()==0)
		{
			identify = (String) se.getAttribute("identify");
		}
		DBCon dbCon = new DBCon();
		try{
			sql1 = "Update ebit91 SET `name`=?,`workID`=?,`Dname`=?,`Pname`=?,`Pstatus`=?,`sex`=?,`nation`=?,"
					+ "`education`=?,`Mstatus`=?,`address`=?,`phone`=?,`identify`=?"
					+ "where `workID`=\"" + workID +"\"";
			sql2 = "Update emst91 SET `Mstatus`=\""+Mstatus+"\" where workID=\""+workID+"\"";
			Connection conn = dbCon.getConnection();
			PreparedStatement pst = conn.prepareStatement(sql1);
			PreparedStatement pst2 = conn.prepareStatement(sql2);
			//prepareStatement执行sql语句
			
			pst.setString(1,name);
			pst.setString(2,workID);
			pst.setString(3,Dname);
			pst.setString(4,Pname);
			pst.setString(5,Pstatus);
			pst.setString(6,sex);
			pst.setString(7,nation);
			pst.setString(8,education);
			pst.setString(9,Mstatus);
			pst.setString(10,address);
			pst.setString(11,phone);
			pst.setString(12,identify);
			pst.executeUpdate();
			pst2.executeUpdate();
			
			pst.close();
			pst2.close();
			conn.close();
			
			//更新session存储的数据用于在报名页面中显示所有的数据库的信息
			Se_Out show = new Se_Out(workID, se);
			response.setHeader("refresh", "1;url=EBIChange.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
